#include "Breakthrough.h"

Breakthrough::Breakthrough()
{
	m_level = 0;
}

Breakthrough::~Breakthrough()
{
}

void Breakthrough::start()
{
	m_board.initialize();

	// Get player piece locations.
	m_state.startState(m_board.getGrid());
	
}

void Breakthrough::legal()
{
	std::vector<Move> legalMoves = m_state.getLegalMoves(m_board.getGrid());
	std::cout << legalMoves.size() << '\n';
	for (int i = 0; i < legalMoves.size(); ++i)
	{
		std::cout << "[(" << legalMoves[i].fromX << "," << legalMoves[i].fromY << ")] -> [(" 
				  << legalMoves[i].toX << "," << legalMoves[i].toY << ")]\n";
	}
}

void Breakthrough::make(int fromX, int fromY, int toX, int toY)
{
	m_state.movePiece(m_board.getGrid(), fromX, fromY, toX, toY);
}

void Breakthrough::retract()
{
	m_state.retract(m_board.getGrid());
}

void Breakthrough::display()
{
	m_board.output();
	std::cout << m_state.getNumMoves() << '\n';
	m_state.printPieces();
}

void Breakthrough::evaluate()
{
	std::cout << "Eval for position: " << m_state.getEvaluation() << '\n';
}

void Breakthrough::go()
{
	//std::vector<Move> moves = m_state.getLegalMoves(m_board.getGrid());
	if (m_level > 0)
	{
		int minimaxValue = m_minimax.miniMax(m_level, m_state, m_board.getGrid());
		Move best = m_minimax.getBestMove();
		std::cout << best.fromX << ", " << best.fromY << " -> " << best.toX << "," << best.toY << '\n';
		m_state.movePiece(m_board.getGrid(), best.fromX, best.fromY, best.toX, best.toY);
	}
	else
	{
		srand((unsigned)time(NULL));
		std::vector<Move> legalMoves = m_state.getLegalMoves(m_board.getGrid());
		int d = rand()%legalMoves.size();
		m_state.movePiece(m_board.getGrid(), legalMoves[d].fromX, legalMoves[d].fromY, 
						  legalMoves[d].toX, legalMoves[d].toY);
	}
}

void Breakthrough::level(std::string level)
{
	if (level == "easy")
	{
		m_level = 0;
	}
	else if (level == "medium")
	{
		m_level = 2;
	}
	else if (level == "hard")
	{
		m_level = 3;
	}
	else if (level == "random")
	{
		m_level = -1;
	}
}

void Breakthrough::debug()
{
}

std::string Breakthrough::getName()
{
	return "Breakthrough";
}